package com.huiyou.service;


import com.huiyou.base.AppBack;
import com.huiyou.base.BaseService;
import com.huiyou.mapper.UserVerifyMapper;
import com.huiyou.model.UserVerifyModel;

import io.swagger.models.auth.In;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/8/15.
 */
@Service
public class UserVerifyService extends BaseService<UserVerifyModel, UserVerifyMapper> {
    private Logger logger = Logger.getLogger(UserVerifyService.class);
    private static String appkey = "24623932";
    private static String secret = "dc4ceeffb6d121f2f068ab35e28c8961";
    private static String url = "http://gw.api.taobao.com/router/rest";//api请求地址


    @Autowired
    private UserVerifyMapper userverifyMapper;


    public AppBack send(String phone, Integer type) {
        String code = (int) (Math.random() * 9000 + 1000) + "";
        UserVerifyModel model = new UserVerifyModel();
        model.setPhone(phone);
        model.setCode(code);
        model.setType(type);

//        if (!sendSDK(phone, code)) {
//            return new AppBack<>(-1, "发送短信失败");
//        }
        int i = userverifyMapper.create(model);
        if (i == 0) {
            return new AppBack<>(-1, "添加失败");
        }
        logger.info("发送短信成功");
        /**
         * 修改
         */
        return new AppBack(model);

    }


//    public boolean sendSDK(String phone, String code) {
//        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
//        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
//        req.setExtend("1");
//        //必须填写normal
//        req.setSmsType("normal");
//        //你应用的名字
//        req.setSmsFreeSignName("金善瑜伽");
//        //电话号码
//        req.setRecNum(phone);
//        //模板
//        req.setSmsTemplateCode("SMS_96090019");
//        //模板中的参数，按照实际情况去
//        req.setSmsParamString("{name:'" + code + "'}");
//
//        try {
//            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
//            // 这里是我设置的一个保存验证码 机制。按照实际需求，自行设计
//            logger.info(rsp.getBody());
//            if (rsp.getResult().getSuccess())
//                return true;
//            else
//                return false;
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }

}
